﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.DataFetcher;

namespace AgencyManagerSystem.Core
{
    public class SupplierUpdaterCreator : BaseUpdaterCreator<SupplierModel>
    {
        SupplierFetcher supplierFetcher = new SupplierFetcher();
        public SupplierUpdater CreateSupplierUpdaterInstance()
        {
            SupplierUpdater supplierUpdater = new SupplierUpdater(supplierFetcher);
            supplierUpdater.DataUpdating += FlagAndApproveFlagValid;
            supplierUpdater.DataUpdating += NotSpaceCheck;
            supplierUpdater.DataUpdating += CodeUniqueCheck;
            supplierUpdater.DataDeleting += MemberExistCheck;
            supplierUpdater.DataDeleting += PuOrderExists;
            supplierUpdater.DataUpdating += SalesCheck;
            return supplierUpdater;
        }

        private void PuOrderExists(BaseUpdater<SupplierModel> sender, DataDeletingEventArgs<SupplierModel> e)
        {
            if (e.Delete == null) return;
            var puOrderFetcher = new PuOrderFetcher();
            var puOrders = puOrderFetcher.ListBySupplier(e.Delete.AutoId);
            if (puOrders.Count>0)
            {
                e.Message = String.Format("该供应商下已经存在{0}等{1}笔订单", puOrders[0].AutoId, puOrders.Count);
                e.Cancel = true;
                return;
            }
        }

        private void SalesCheck(BaseUpdater<SupplierModel> sender, DataUpdatingEventArgs<SupplierModel> e)
        {
            if (e.Insert == null) return;
            var usrFetcher = new UserFetcher();
            var user = usrFetcher.Get(e.Insert.Sales);
            if(user == null)
            {
                e.Message = String.Format("业务员{0}不存在", e.Insert.Sales);
                e.Cancel = true;
                return;
            }
        }

        private void MemberExistCheck(BaseUpdater<SupplierModel> sender, DataDeletingEventArgs<SupplierModel> e)
        {
            if (e.Delete == null) return;
            SupplierMemberFetcher fetcher = new SupplierMemberFetcher();
            var member = fetcher.ListBySupplier(e.Delete.AutoId);
            if(member.Count>0)
            {
                e.Message = String.Format("[{0}]已存在成员{1}等{2}个记录", e.Delete.Code,member[0].AutoId,member.Count);
                e.Cancel = true;
                return;
            }
        }

        private void CodeUniqueCheck(BaseUpdater<SupplierModel> sender, DataUpdatingEventArgs<SupplierModel> e)
        {
            if (e.Insert == null) return;
            var supplierFetcher = new SupplierFetcher();
            var supplier = supplierFetcher.GetByCode(e.Insert.Code);
            if (supplier == null) return;
            if (supplier.Code==e.Insert.Code&&supplier.AutoId!=e.Insert.AutoId)
            {
                e.Message = String.Format("代号[{0}]已经存在于单据{1}",e.Insert.Code,supplier.AutoId);
                e.Cancel = true;
                return;
            }
        }

        private void NotSpaceCheck(BaseUpdater<SupplierModel> sender, DataUpdatingEventArgs<SupplierModel> e)
        {
            if (e.Insert == null) return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)|| String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "代号或者名称不能空白";
                e.Cancel = true;
                return;
            }
        }

    }
}